package com.ruoyi.surfacewater.service.impl;

import com.ruoyi.common.utils.BankerSRoundingUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.surfacewater.mapper.NutritionalIndexMapper;
import com.ruoyi.surfacewater.service.NutritionalIndexService;
import com.ruoyi.utli.SegmentFormula;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class NutritionalIndexServiceImpl implements NutritionalIndexService {

    @Autowired
    private NutritionalIndexMapper nutritionalIndexMapper;

    @Autowired
    private SegmentFormula segmentFormula;


    @Override
    public List<Map<String, Object>> nutritionalIndexlist(Map<String, Object> map) {
        List<Map<String, Object>> list=new ArrayList<>();
        Object segmentType = map.get("segmentType");
        Object SegmentIDs = map.get("SegmentIDs");
        //   Object itemCodes = map.get("itemCodes");
        Object startTime = map.get("startTime");
        Object endTime = map.get("endTime");
        if(SegmentIDs!=null&&!"".equals(SegmentIDs)){
            List<String> SegmentIDlist= (List<String>) SegmentIDs;
            List<Map<String, Object>> listdata =new ArrayList<>();
            if(segmentType!=null&&"0".equals(segmentType)){
                listdata = nutritionalIndexMapper.automonitorHourdatalakeNutritionalStatus(map);
            }
            if(segmentType!=null&&"1".equals(segmentType)){
                listdata = nutritionalIndexMapper.automonitorDaydatalakeNutritionalStatus(map);
            }
            if(segmentType!=null&&"2".equals(segmentType)){
                listdata = nutritionalIndexMapper.automonitorMonthdatadatalakeNutritionalStatus(map);
            }
            if(listdata!=null&&listdata.size()>0){
                List<String> sampleTimelist=new ArrayList<>();
                //获取时间
                for(Map<String, Object> listdatas : listdata){
                    Object sampleTime = listdatas.get("SampleTime");//时间
                    Object sampleTimehore = listdatas.get("SampleTimehore");//时间 到小时
                    Object sampleTimeday = listdatas.get("SampleTimeday");//时间 到日
                    Object SampleTimemon = listdatas.get("SampleTimemon");//时间 到日
                    if(segmentType!=null&&"0".equals(segmentType)){
                        boolean contains = sampleTimelist.contains(sampleTimehore);
                        if(contains){}else {
                            sampleTimelist.add(String.valueOf(sampleTimehore));
                        }
                    }
                    if(segmentType!=null&&"1".equals(segmentType)){
                        boolean contains = sampleTimelist.contains(sampleTimeday);
                        if(contains){}else {
                            sampleTimelist.add(String.valueOf(sampleTimeday));
                        }
                    }
                    if(segmentType!=null&&"2".equals(segmentType)){
                        boolean contains = sampleTimelist.contains(SampleTimemon);
                        if(contains){}else {
                            sampleTimelist.add(String.valueOf(SampleTimemon));
                        }
                    }
                }

                //查询参与计算因子
                Map NIcodBussinessitemsmap=new HashMap();
                NIcodBussinessitemsmap.put("BussinessCategoryCode","NI");
                List<Map<String, Object>> NIcodBussinessitems = nutritionalIndexMapper.NIcodBussinessitems(NIcodBussinessitemsmap);

                if(sampleTimelist!=null&&sampleTimelist.size()>0){
                    for(String dataTime: sampleTimelist){
                        if(SegmentIDlist!=null&&SegmentIDlist.size()>0){
                            for(String data: SegmentIDlist){
                                Map<String,Object> datamap=new HashMap<>();
                                List<String> nutrientWaterQualityIndexitemlist=new ArrayList<>();//营养化指数
                                if(NIcodBussinessitems!=null&&NIcodBussinessitems.size()>0){
                                    for(Map<String, Object> itemdata: NIcodBussinessitems) {
                                        Object itemCode = itemdata.get("ItemCode");
                                        Object itemName = itemdata.get("ItemName");
                                        List<Map<String, Object>> sunmapsLevel =new ArrayList<>();
                                        if(segmentType!=null&&"0".equals(segmentType)){
                                            sunmapsLevel = listdata.stream()
                                                    .filter(item -> item.get("id").equals(data)
                                                            && item.get("SampleTimehore").equals(dataTime)
                                                            && item.get("ItemCode").equals(itemCode)
                                                    )
                                                    .collect(Collectors.toList());
                                        }
                                        if(segmentType!=null&&"1".equals(segmentType)){
                                            sunmapsLevel = listdata.stream()
                                                    .filter(item -> item.get("id").equals(data)
                                                            && item.get("SampleTimeday").equals(dataTime)
                                                            && item.get("ItemCode").equals(itemCode)
                                                    )
                                                    .collect(Collectors.toList());
                                        }
                                        if(segmentType!=null&&"2".equals(segmentType)){
                                            sunmapsLevel = listdata.stream()
                                                    .filter(item -> item.get("id").equals(data)
                                                            && item.get("SampleTimemon").equals(dataTime)
                                                            && item.get("ItemCode").equals(itemCode)
                                                    )
                                                    .collect(Collectors.toList());
                                        }
                                        if(sunmapsLevel!=null&&sunmapsLevel.size()>0){
                                            for(Map<String, Object> listdatas : sunmapsLevel){
                                                Object id = listdatas.get("id");//断面id
                                                Object segmentName = listdatas.get("SegmentName");//断面名称
                                                Object segmentCategory = listdatas.get("SegmentCategory");//断面分类（1河流/2湖库/3地下水）
                                                Object lakeID = listdatas.get("LakeID");//湖库id
                                                Object lakeName = listdatas.get("LakeName");//湖库名称
                                                Object sampleTime = listdatas.get("SampleTime");//时间
                                                Object sampleTimehore = listdatas.get("SampleTimehore");//时间 到小时
                                                Object sampleTimeday = listdatas.get("SampleTimeday");//时间 到日
                                                Object SampleTimemon = listdatas.get("SampleTimemon");//时间 到日
                                                Object effectiveValue = listdatas.get("EffectiveValue");//均值
                                                Object keepPointBits = listdatas.get("KeepPointBits");//保留位数
                                                Object targetLevelNames = listdatas.get("targetLevelName");//目标水质级别
                                                Object targetLevels = listdatas.get("targetLevel");//目标水质级别
                                                if(segmentCategory!=null&&"2".equals(String.valueOf(segmentCategory))){
                                                    datamap.put("segmentID",id);
                                                    datamap.put("segmentName",segmentName);
                                                    datamap.put("segmentCategory",segmentCategory);
                                                    datamap.put("lakeID",lakeID);
                                                    datamap.put("lakeName",lakeName);

                                                    if(segmentType!=null&&"0".equals(segmentType)){
                                                        datamap.put("sampleTime",sampleTimehore);
                                                    }
                                                    if(segmentType!=null&&"1".equals(segmentType)){
                                                        datamap.put("sampleTime",sampleTimeday);
                                                    }  if(segmentType!=null&&"2".equals(segmentType)){
                                                        datamap.put("sampleTime",SampleTimemon);
                                                    }
                                                    if(effectiveValue!=null&&!"".equals(effectiveValue)){
                                                        //判断是否科学计算法，如果是就进行转换
                                                        Integer keepPointBitss=3;
                                                        if(keepPointBits!=null&&!"".equals(keepPointBits)){
                                                            keepPointBitss=Integer.valueOf(String.valueOf(keepPointBits));
                                                        }
                                                        boolean scientificNotation = StringUtils.isScientificNotation(String.valueOf(effectiveValue));
                                                        if (scientificNotation) {
                                                            BigDecimal bigDecimal = new BigDecimal(String.valueOf(effectiveValue));
                                                            String effectiveValuesdatas = bigDecimal.toPlainString();
                                                            effectiveValue = effectiveValuesdatas;
                                                        } else {
                                                            effectiveValue = String.valueOf(effectiveValue);
                                                        }
                                                        //单因子营养水质指数
                                                        String nutrientWaterQualityIndexitems = segmentFormula.nutrientWaterQualityIndexitemtel(String.valueOf(itemCode), String.valueOf(effectiveValue));
                                                        String nutrientWaterQualityIndexitemszj = segmentFormula.nutrientWaterQualityIndexitem(String.valueOf(itemCode), String.valueOf(effectiveValue));
                                                        if(nutrientWaterQualityIndexitems!=null&&!"".equals(nutrientWaterQualityIndexitems)){
                                                            if(nutrientWaterQualityIndexitemszj!=null&&!"".equals(nutrientWaterQualityIndexitemszj)){
                                                                nutrientWaterQualityIndexitemlist.add(nutrientWaterQualityIndexitemszj);
                                                            }
                                                            datamap.put(itemCode+"_int", BankerSRoundingUtils.sciCal(Double.parseDouble(nutrientWaterQualityIndexitems), 4));
                                                            datamap.put(itemCode+"_intvlue",nutrientWaterQualityIndexitemszj);
                                                        }else {
                                                            datamap.put(itemCode+"_int",null);
                                                        }
                                                        BigDecimal bigDecimal = new BigDecimal(String.valueOf(effectiveValue)).setScale(keepPointBitss, BigDecimal.ROUND_HALF_UP);
                                                        datamap.put(itemCode+"_val",bigDecimal+"");

                                                    }
                                                }
                                            }
                                        }

                                    }
                                }

                                if(nutrientWaterQualityIndexitemlist!=null&&nutrientWaterQualityIndexitemlist.size()>0){
                                    String tbindex = segmentFormula.riverWaterQualityIndex(nutrientWaterQualityIndexitemlist);
                                    datamap.put("waterQualityIndex", tbindex);
                                    String s = segmentFormula.nutrientWaterQualityIndexclassification(Double.valueOf(tbindex));
                                    datamap.put("waterQualityIndexclassification", s);
                                }
                                if(datamap!=null&&datamap.size()>0){
                                    list.add(datamap);
                                }
                            }
                        }
                    }
                }
            }
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> nutritionalStatusintlist(Map<String, Object> map) {
        List<Map<String, Object>> list=new ArrayList<>();
        Object segmentType = map.get("segmentType");
        Object SegmentIDs = map.get("SegmentIDs");
        //   Object itemCodes = map.get("itemCodes");
        Object startTime = map.get("startTime");
        Object endTime = map.get("endTime");
        if(SegmentIDs!=null&&!"".equals(SegmentIDs)){
            List<String> SegmentIDlist= (List<String>) SegmentIDs;
            List<Map<String, Object>> listdata =nutritionalIndexMapper.integrationmonitordataLakeNutritionalStatus(map);
            if(listdata!=null&&listdata.size()>0){
                List<String> sampleTimelist=new ArrayList<>();
                //获取时间
                for(Map<String, Object> listdatas : listdata){

                    Object SampleTimemon = listdatas.get("SampleTimemon");//时间 到日
                    boolean contains = sampleTimelist.contains(SampleTimemon);
                    if(contains){}else {
                        sampleTimelist.add(String.valueOf(SampleTimemon));
                    }
                }
                //查询参与计算因子
                Map NIcodBussinessitemsmap=new HashMap();
                NIcodBussinessitemsmap.put("BussinessCategoryCode","NI");
                List<Map<String, Object>> NIcodBussinessitems = nutritionalIndexMapper.NIcodBussinessitems(NIcodBussinessitemsmap);

                if(sampleTimelist!=null&&sampleTimelist.size()>0){
                    for(String dataTime: sampleTimelist){
                        if(SegmentIDlist!=null&&SegmentIDlist.size()>0){
                            for(String data: SegmentIDlist){
                                Map<String,Object> datamap=new HashMap<>();
                                List<String> nutrientWaterQualityIndexitemlist=new ArrayList<>();//营养化指数
                                if(NIcodBussinessitems!=null&&NIcodBussinessitems.size()>0){
                                    for(Map<String, Object> itemdata: NIcodBussinessitems) {
                                        Object itemCode = itemdata.get("ItemCode");
                                        Object itemName = itemdata.get("ItemName");
                                        List<Map<String, Object>> sunmapsLevel =new ArrayList<>();
                                        sunmapsLevel = listdata.stream()
                                                .filter(item -> item.get("id").equals(data)
                                                        && item.get("SampleTimemon").equals(dataTime)
                                                        && item.get("ItemCode").equals(itemCode)
                                                )
                                                .collect(Collectors.toList());

                                        if(sunmapsLevel!=null&&sunmapsLevel.size()>0){
                                            for(Map<String, Object> listdatas : sunmapsLevel){
                                                Object id = listdatas.get("id");//断面id
                                                Object segmentName = listdatas.get("SegmentName");//断面名称
                                                Object segmentCategory = listdatas.get("SegmentCategory");//断面分类（1河流/2湖库/3地下水）
                                                Object lakeID = listdatas.get("LakeID");//湖库id
                                                Object lakeName = listdatas.get("LakeName");//湖库名称
                                                Object SampleTimemon = listdatas.get("SampleTimemon");//时间
                                                Object effectiveValue = listdatas.get("EffectiveValue");//均值
                                                Object keepPointBits = listdatas.get("KeepPointBits");//保留位数
                                                Object targetLevelNames = listdatas.get("targetLevelName");//目标水质级别
                                                Object targetLevels = listdatas.get("targetLevel");//目标水质级别
                                                if(segmentCategory!=null&&"2".equals(String.valueOf(segmentCategory))){
                                                    datamap.put("segmentID",id);
                                                    datamap.put("segmentName",segmentName);
                                                    datamap.put("segmentCategory",segmentCategory);
                                                    datamap.put("lakeID",lakeID);
                                                    datamap.put("lakeName",lakeName);

                                                    datamap.put("sampleTime",SampleTimemon);
                                                    if(effectiveValue!=null&&!"".equals(effectiveValue)){
                                                        //判断是否科学计算法，如果是就进行转换
                                                        Integer keepPointBitss=3;
                                                        if(keepPointBits!=null&&!"".equals(keepPointBits)){
                                                            keepPointBitss=Integer.valueOf(String.valueOf(keepPointBits));
                                                        }
                                                        boolean scientificNotation = StringUtils.isScientificNotation(String.valueOf(effectiveValue));
                                                        if (scientificNotation) {
                                                            BigDecimal bigDecimal = new BigDecimal(String.valueOf(effectiveValue));
                                                            String effectiveValuesdatas = bigDecimal.toPlainString();
                                                            effectiveValue = effectiveValuesdatas;
                                                        } else {
                                                            effectiveValue = String.valueOf(effectiveValue);
                                                        }
                                                        //单因子营养水质指数
                                                        String nutrientWaterQualityIndexitems = segmentFormula.nutrientWaterQualityIndexitemtel(String.valueOf(itemCode), String.valueOf(effectiveValue));
                                                        String nutrientWaterQualityIndexitemszj = segmentFormula.nutrientWaterQualityIndexitem(String.valueOf(itemCode), String.valueOf(effectiveValue));
                                                        if(nutrientWaterQualityIndexitems!=null&&!"".equals(nutrientWaterQualityIndexitems)){
                                                            if(nutrientWaterQualityIndexitemszj!=null&&!"".equals(nutrientWaterQualityIndexitemszj)){
                                                                nutrientWaterQualityIndexitemlist.add(nutrientWaterQualityIndexitemszj);
                                                            }
                                                            datamap.put(itemCode+"_int",BankerSRoundingUtils.sciCal(Double.parseDouble(nutrientWaterQualityIndexitems), 4));
                                                        }else {
                                                            datamap.put(itemCode+"_int",null);
                                                        }
                                                        BigDecimal bigDecimal = new BigDecimal(String.valueOf(effectiveValue)).setScale(keepPointBitss, BigDecimal.ROUND_HALF_UP);
                                                        datamap.put(itemCode+"_val",bigDecimal+"");

                                                    }
                                                }
                                            }
                                        }

                                    }
                                }

                                if(nutrientWaterQualityIndexitemlist!=null&&nutrientWaterQualityIndexitemlist.size()>0){
                                    String tbindex = segmentFormula.riverWaterQualityIndex(nutrientWaterQualityIndexitemlist);
                                    datamap.put("waterQualityIndex", tbindex);
                                    String s = segmentFormula.nutrientWaterQualityIndexclassification(Double.valueOf(tbindex));
                                    datamap.put("waterQualityIndexclassification", s);

                                }
                                if(datamap!=null&&datamap.size()>0){
                                    list.add(datamap);
                                }
                            }
                        }
                    }
                }
            }
        }
        return list;
    }
}
